package org.wukl.vhr.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wukl.vhr.mapper.HrMapper;
import org.wukl.vhr.mapper.HrRoleMapper;
import org.wukl.vhr.model.Hr;
import org.wukl.vhr.utils.HrUtils;

import java.util.List;

@Service
public class HrService implements UserDetailsService {
    @Autowired
    HrMapper hrMapper;
    @Autowired
    HrRoleMapper hrRoleMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       Hr hr=hrMapper.loadUserByUsername(username);
       if(hr==null)
       {
           throw new UsernameNotFoundException("用户名不存在");
       }
       hr.setRoles(hrMapper.getRolesByHrId(hr.getId()));
        return hr;
    }
    public List<Hr> queryAllHr(String keywords)
    {

        return hrMapper.queryAllHr(HrUtils.getCurrentLoginHr().getId(),keywords);
    }
    public Integer updateHr(Hr hr)
    {
        return hrMapper.updateByPrimaryKeySelective(hr);
    }
    @Transactional
    public Boolean updateHrRoles(Integer hrId,Integer[] rolesId)
    {
        hrRoleMapper.deleteByHrId(hrId);
        return hrRoleMapper.insertHrRoles(hrId,rolesId)==rolesId.length;
    }
    public Integer deleteHr(int id)
    {
        return hrMapper.deleteByPrimaryKey(id);
    }
}
